package com.lokiy.kit.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import androidx.viewpager2.widget.ViewPager2
import com.lokiy.annotation.FragmentInfo
import com.lokiy.core.adapter.ViewBindingAdapter
import com.lokiy.core.fragment.ViewBindingFragment
import com.lokiy.kit.R
import com.lokiy.kit.databinding.FragmentViewPager2Binding
import com.lokiy.kit.databinding.FragmentViewPager2ItemBinding
import com.lokiy.kit.ext.setupToolbar
import kotlin.math.abs
import kotlin.random.Random

/**
 *
 * @author Lokiy
 * @date 2023-10-08 17:16
 */
@FragmentInfo("ViewPage2 滑动拦截")
class ViewPage2Fragment : ViewBindingFragment<FragmentViewPager2Binding>() {

    private var isIntercept = false
    private var isIn = false
    private var downX = 0f
    private var downY = 0f

    @SuppressLint("ClickableViewAccessibility")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        setupToolbar(binding.toolbar)
        binding.viewPager2.adapter = ViewBindingAdapter<Int, FragmentViewPager2ItemBinding> { bindingHolder, item ->
            bindingHolder.binding.imageView.setImageResource(item)
            bindingHolder.binding.text.text = bindingHolder.adapterPosition.toString()
        }.also {
            it.refresh(mutableListOf<Int>().apply { repeat(100) { add(if (Random.nextBoolean()) R.drawable.a1 else R.drawable.a2) } })
        }
        binding.viewPager2.orientation = ViewPager2.ORIENTATION_VERTICAL
        binding.viewPager2.requestDisallowInterceptTouchEvent(true)
        binding.viewPager2.currentItem = 10
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        if (ev?.pointerCount == 1) {
            when (ev.action) {
                MotionEvent.ACTION_DOWN -> {
                    isIn = binding.viewPager2.currentItem < 3
                    downX = ev.rawX
                    downY = ev.rawY
                }

                MotionEvent.ACTION_MOVE -> {
                    if (!isIntercept) {
                        if (isIn) {
                            val dy = abs(ev.rawY - downY)
                            val dx = abs(ev.rawX - downX)
                            if (dy > dx && dy > 10 && dx < 100) {
                                if (ev.rawY - downY > 0) {
                                    isIntercept = true
                                    binding.tips.animate().cancel()
                                    binding.tips.animate().alpha(1f).setStartDelay(0).start()
                                }
                            }
                        }
                    }
                }

                MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                    isIn = false
                    isIntercept = false
                    binding.tips.animate().cancel()
                    binding.tips.animate().alpha(0f).setStartDelay(3000).start()
                }
            }
        }
        return isIntercept
    }

}